package com.java9s.goldchit.controllers;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.java9s.goldchit.beans.Chit;
import com.java9s.goldchit.beans.ChitPaymentDetails;
import com.java9s.goldchit.beans.Customer;
import com.java9s.goldchit.beans.CustomerChit;
import com.java9s.goldchit.dao.CustomerDao;
import com.java9s.goldchit.dao.CustomerDaoImpl;

@Controller
public class CutomerController implements ApplicationContextAware {

	private ApplicationContext applicationContext;
	private Validator validator;
	private CustomerDao customerDao;

	public CustomerDao getCustomerDao() {
		if (customerDao == null) {
			customerDao = (CustomerDaoImpl) applicationContext
					.getBean("customerDao");
		}
		return customerDao;
	}

	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}

	public CutomerController() {
		ValidatorFactory validatorFactory = Validation
				.buildDefaultValidatorFactory();
		validator = validatorFactory.getValidator();
	}

	@RequestMapping(value = "goldChitHome", method = RequestMethod.GET)
	public String getHomePage(Model model) {
		return "home-tile";
	}

	@RequestMapping(value = "addCustomer", method = RequestMethod.GET)
	public String getRegisterCustomer(Model model) {

		Customer customer = (Customer) applicationContext.getBean("customer");
		setCustomerChits(customer);
		model.addAttribute("customerAttribute", customer);
		return "register-customer-tile";
	}

	@RequestMapping(value = "addCustomer", method = RequestMethod.POST)
	public String registerCustomer(
			@ModelAttribute("customerAttribute") Customer customer,
			BindingResult bindingResult, Model model) {

		// beanvalidation(customer, bindingResult);

		if (bindingResult.hasErrors()) {
			model.addAttribute("model", model);
		} else {
			customer.setPassword("om39a");
			getCustomerDao().saveCustomer(customer);
			model.addAttribute("addedCustomer", customer);
		}
		return "register-customer-tile";
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}

	private void beanvalidation(Customer customer, BindingResult bindingResult) {

		Set<ConstraintViolation<Customer>> customerConstraintViolations = validator
				.validate(customer);

		for (ConstraintViolation<Customer> constraintViolation : customerConstraintViolations) {
			String propertyName = constraintViolation.getPropertyPath()
					.toString();
			String description = constraintViolation.getMessage();
			bindingResult.addError(new FieldError("customerAttribute",
					propertyName, description));
		}
	}

	private void setCustomerChits(Customer customer) {

		ChitPaymentDetails chitPaymentDetails = new ChitPaymentDetails();
		chitPaymentDetails.setAmountPaid(2000);
		chitPaymentDetails.setCardNumber("5654621564862");
		chitPaymentDetails.setGoldAllocatedInGms(.51455);
		chitPaymentDetails.setInstallmentNumber((short) 1);
		chitPaymentDetails.setModeOfPayment("card");
		chitPaymentDetails.setPaymentDate(new Date());

		Set<ChitPaymentDetails> chitPaymentDetailset = new HashSet<ChitPaymentDetails>();
		chitPaymentDetailset.add(chitPaymentDetails);

		Chit chitType = new Chit();
		chitType.setChitName("Chit 001");
		chitType.setChitNumber(1);
		chitType.setGoldKarat((short) 22);
		chitType.setAmountLimit(20000);
		chitType.setMaxMonths((short) 12);

		CustomerChit customerChit = new CustomerChit();
		customerChit.setChitType(chitType);
		customerChit.setChitPaymentDetails(chitPaymentDetailset);
		customerChit.setCustomerChitNumber(1);
		customerChit.setStatus("paid");

		Set<CustomerChit> customerChits = new HashSet<CustomerChit>();
		customerChits.add(customerChit);

		customer.setCustomerChits(customerChits);
	}
}
